Imports adsiClass
Imports System.Data
Imports System.Data.OleDb
Imports System.DirectoryServices
Imports System.Security.Cryptography

Partial Class wpAdminLogin
    Inherits System.Web.UI.Page

    Dim cr As New Crypto
    Dim ad As New adsiClass

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim conStr As String = ConfigurationManager.ConnectionStrings("ConStr").ToString
            Try
                Dim qry As String
                qry = "select distinct dc_name from dc"
                Dim da As New OleDbDataAdapter
                da = New OleDbDataAdapter(qry, conStr.ToString)
                Dim ds As New DataSet
                da.Fill(ds)
                Dim dt As New DataTable
                dt = ds.Tables(0)
                Me.cboDC.DataSource = dt
                Me.cboDC.DataTextField = "dc_name"
                Me.cboDC.DataBind()

                ''''''''''''''''''''''''''''''''''''''''

            Catch ex As Exception
                ad.SaveTextToFile(Now.ToLongDateString & " " & Now.ToLongTimeString & vbNewLine & ex.Message.ToString & vbNewLine & ex.ToString & vbNewLine & vbNewLine & ex.StackTrace.ToString & vbNewLine & vbNewLine & "DB Connectivity Error" & vbNewLine & "Connection String --> " & conStr.ToString)
                Session("msg") = "Error in connecting to database. Please contact your system administrator..."
                Response.Redirect("~/wpMessage.aspx")
            End Try
        End If
        '''''''''''''''''''

    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSignIn.Click
        Session("dc") = cr.Encrypt(Me.cboDC.Text.ToString, "key1!")
        Session("user") = cr.Encrypt(Me.txtUserName.Text.ToString, "key1!")
        Session("pas") = cr.Encrypt(Me.txtPassword.Text.ToString, "key1!")

        Dim LoginName As String = ad.ExtractUserNameFromLoginName(Me.txtUserName.Text.ToString)
        Dim result As SearchResult
        ad.userName = cr.Decrypt(Session("user").ToString(), "key1!")
        ad.userPassword = cr.Decrypt(Session("pas").ToString(), "key1!")
        '''''''Declare Username and Password before you make this object...
        Dim de As DirectoryEntry = ad.GetDirectoryEntry("LDAP://" & cr.Decrypt(Session("dc").ToString, "key1!") & "")
        Dim dsh As New DirectorySearcher(de)
        dsh.SearchScope = SearchScope.Subtree
        dsh.Filter = "(&(objectClass=user) (sAMAccountName=" & LoginName & "))"
        Try
            result = dsh.FindOne()
            Dim isAdmin As Boolean = ad.UserExistInGroup(de, result.Path, "Domain Admins")
            If isAdmin = True Then
                Response.Redirect("~/wpAdminSearch.aspx")
            ElseIf isAdmin = False Then
                Me.lblErrorMsg.Visible = True
                Me.lblErrorMsg.Text = "Sorry you are not authorized to login on this page"
            End If
        Catch ex As Exception

        End Try


        '''''''''''''''''''''''''
    End Sub

End Class
